<?php
/*
 * Session variables:
 * $_SESSION['database'] [string] = database name of the current tutorial centre
 * $_SESSION['type'] [string] = user type of the logged in user
 * $_SESSION['authorised'] [boolean] = whether or not the current user is logged in
 * $_SESSION['user'] [string] = the username/ email of the logged in user
 */
session_start();
if(isset($_SESSION['authorised']) && $_SESSION['authorised'] == true)
{
	if(isset($_SESSION['type']))
	{
		if($_SESSION['type'] == 'administrator')
		{
			header('Location: admin_index.php');
			exit();
		}
		elseif($_SESSION['type'] == 'student')
		{
			header('Location: student_index.php');
			exit();
		}
		elseif($_SESSION['type'] == 'tutor')
		{
			header('Location: tutor_index.php');
			exit();
		}
		elseif($_SESSION['type'] == 'parent')
		{
			header('Location: parent_index.php');
			exit();
		}
	}
}
else
{
	if(isset($_POST['tcname']))
	{
		$host = 'localhost';
		$dbuser = 'postgres';
		$dbpass = '1190';
		$dbname = 'tbms_databases';
		include('connect.inc');

		$tutorial_centre = strtoupper(pg_escape_string($_POST['tcname']));
		$sql = "SELECT * FROM databases WHERE tutorial_centre = '$tutorial_centre';";
		$result = pg_query($sql);
		$count = pg_num_rows($result);

		if($count >= 1)
		{
			$_SESSION['signupmessage'] = 'Tutorial centre already exists.';
			header('Location: signup.php');
		}
		else
		{
			if($_POST['tcname'] == null || $_POST['tcname'] == "")
			{
				$_SESSION['signupmessage'] = 'Please enter the tutorial centre name';
				header('Location: signup.php');
				exit();
			}
			else
			{
				$_SESSION['tcname'] =  $tutorial_centre;
				?>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
	    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	    <title>Sign Up</title>
	    <link href="StyleSheet.css" rel="stylesheet" type="text/css" />
	    <style>
	        .nav {
		    width:210px;
		    height:auto;
		    border:thin solid #09C;
		    margin-left: 10px;
		    text-align : center;}

	        .mainline {
		    width:712px;
		    height:auto;
            text-align : center;}
    	</style>
	</head>


    <center>
    <body bgcolor="#FFFFFF">
		<div id="wrapper">
            <div id="header">
            <h1></h1>
            </div>

            <div id="header2">
                <h3></h3>
            </div>

			<div id="nav">
				<?php include('signnav.php');?>
			</div>

			<div id="mainline">
				<h4>Sign Up</h4>
			</div>
			<div id="body">
					<?php
					include('connect.inc');

						// table name 
						$tbl_name='temp_members';

						// Random confirmation code 
						$confirm_code=md5(uniqid(rand()));

						// values sent from form 
						$lastname=strtoupper(addslashes($_POST['owner_last_name']));
						$firstname=strtoupper(addslashes($_POST['owner_first_name']));
						$middlename=strtoupper(addslashes($_POST['owner_middle_name']));
						$email=$_POST['owner_email'];
						$address1=strtoupper(addslashes($_POST['owner_street_add1']));
						$address2=strtoupper(addslashes($_POST['owner_street_add2']));
						$city=strtoupper(addslashes($_POST['owner_city_add']));
						$contact_num=$_POST['owner_contact'];
						$gender=$_POST['gender'];
						$birthmonth=strtoupper(addslashes($_POST['birthmonth']));
						$birthday=$_POST['birthday'];
						$birthyear=$_POST['birthyear'];
						$password=$_POST['owner_password'];
						$password2=$_POST['owner_password2'];
						$tutorial_centre = $_POST['tcname'];
						if(($firstname!=null) AND ($middlename!=null) AND ($lastname!=null) AND ($email!=null) AND ($address1!=null) AND ($address2!=null) AND ($city!=null) AND ($contact_num!=null) AND ($password!=null) AND ($password==$password2))
						{// Insert data into database 
							
							$sql="INSERT INTO $tbl_name(confirm_code, tutorial_centre, lastname, firstname, middlename, email, password, gender, birthday, stadd1, stadd2, city, contact_num)VALUES('$confirm_code', '$tutorial_centre', '$lastname','$firstname','$middlename','$email', '$password', '$gender', to_date('$birthday $birthmonth $birthyear', 'DD MON YYYY'), '$address1','$address2','$city','$contact_num')";
							$result=pg_query($sql);

							// if successfully inserted data into database, send confirmation link to email 
							if($result)
							{

								$message="Hi $firstname!\r\n\r\n";
								$message.="Welcome to Tutorial Business Management System! \r\n";
								$message.="Please click on the link below to activate your account. \r\n\r\n";
								$message.="http://localhost/confirmation.php?passkey=$confirm_code";
								$message.="\r\n\r\nYour registered tutorial centre is $tutorial_centre.\r\n";
								$message.="\r\nYour username is $email.\r\n";
								$message.="Your password is $password.";
								$message.="\r\n\r\nThanks, \r\n\r\n";
								$message.="TBMS team \r\n";
								
								require_once('class.phpgmailer.php');
								$mail = new PHPGMailer();
								$mail->Username = 'cs192tbms@gmail.com'; 
								$mail->Password = 'notnamed';
								$mail->From = 'cs192tbms@gmail.com'; 
								$mail->FromName = 'CS 192 TBMS Group';
								$mail->Subject = 'Welcome to Tutorial Business Management System';
								$mail->AddAddress("$email");
								$mail->Body = "$message";
								$mail->Send();
								
								echo("<p>Your confirmation link has been successfully sent to $email!</p>");
							}
							
							
							// if not found 
							else 
							{
								echo "Sign up unsuccessful";
							}
						}
						else
						{?>
							<p align="left">Please provide all correct details to complete the sign up process</p>
							<div id="mainline">
							<br /><form action="signup2.php" method="post" align="left" name="tutorial_centre_name_form"><input type="submit" id="back" name="back" value="Back"/>
							<input type="hidden" id="tcname" name="tcname" value="$tutorial_centre"/></form>
							</div>
						<?php
						}
						?>
			</div>
				
				
			<p align="justify"><br /></p>
			<br class="clearfloat" />
			<br />

			<div id="footer">
				<p align="center">
					Tutorial Business Manager: info@tbms.com <br />Credits/Engineers: Aquino, Chloebelle; Dela Cruz, Roy Erwin; Maliwanag, Ranyel Bryan; Manzano, Audrey Ann; Ng, Sheila Regine
					<br /><br />
					copyright 2011
				</p>
			</div>
		</div>
	</body>
    </center>
</html>
<?php
}}}}
?>	

